package com.moxiao.yfjh.module.blind.service.memberusertag;

import com.moxiao.yfjh.module.blind.controller.admin.memberlabel.vo.MemberLabelRespVO;
import com.moxiao.yfjh.module.blind.convert.album.BlindMemberAlbumConvert;
import com.moxiao.yfjh.module.blind.convert.memberlabel.MemberLabelConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.album.BlindMemberAlbumDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberlabel.MemberLabelDO;
import com.moxiao.yfjh.module.blind.dal.mysql.memberlabel.MemberLabelMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.moxiao.yfjh.module.blind.controller.admin.memberusertag.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberusertag.MemberUsertagDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.memberusertag.MemberUsertagConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.memberusertag.MemberUsertagMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲用户标签 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberUsertagServiceImpl implements MemberUsertagService {

    @Resource
    private MemberUsertagMapper memberUsertagMapper;
    @Resource
    private MemberLabelMapper memberLabelMapper;

    @Override
    public Long createMemberUsertag(MemberUsertagCreateReqVO createReqVO) {
        // 插入
        MemberUsertagDO memberUsertag = MemberUsertagConvert.INSTANCE.convert(createReqVO);
        memberUsertagMapper.insert(memberUsertag);
        // 返回
        return memberUsertag.getId();
    }

    @Override
    public void createUserTag(List<Long> labelRespVOS, Long userId) {
        if(CollectionUtils.isNotEmpty(labelRespVOS)){
            List<MemberUsertagCreateReqVO> createReqVO = new ArrayList<>();
            labelRespVOS.forEach(id -> {
                MemberUsertagCreateReqVO memberUsertagCreateReqVO = new MemberUsertagCreateReqVO();
                memberUsertagCreateReqVO.setUserId(userId);
                MemberLabelDO memberLabelDO = memberLabelMapper.selectById(id);
                memberUsertagCreateReqVO.setLabel(memberLabelDO.getLabel());
                memberUsertagCreateReqVO.setLabelId(id);
                createReqVO.add(memberUsertagCreateReqVO);
            });
            List<MemberUsertagDO> memberUserTagDOS = MemberUsertagConvert.INSTANCE.convert(createReqVO);
            Collection<MemberUsertagDO> userTagDOS = new ArrayList<>(memberUserTagDOS);
            memberUsertagMapper.insertBatch(userTagDOS);
        }
    }


    @Override
    public void createMemberUserTag(List<MemberUsertagCreateReqVO> createReqVO,Long userId) {
        // 插入
        List<MemberUsertagDO> memberUserTagDOS = MemberUsertagConvert.INSTANCE.convert(createReqVO);
        memberUserTagDOS.forEach(memberUsertagDO -> {
            memberUsertagDO.setUserId(userId);
        });
        Collection<MemberUsertagDO> userTagDOS = new ArrayList<>(memberUserTagDOS);
        memberUsertagMapper.insertBatch(userTagDOS);
    }

    @Override
    public void updateMemberUsertag(MemberUsertagUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberUsertagExists(updateReqVO.getId());
        // 更新
        MemberUsertagDO updateObj = MemberUsertagConvert.INSTANCE.convert(updateReqVO);
        memberUsertagMapper.updateById(updateObj);
    }

    @Override
    public void updateMemberUserTag(List<MemberUsertagCreateReqVO> createReqVO, Long userId) {
        MemberUsertagExportReqVO exportReqVO = new MemberUsertagExportReqVO();
        exportReqVO.setUserId(userId);
        List<MemberUsertagDO> memberUserTagDOS = memberUsertagMapper.selectList(exportReqVO);
        if (CollectionUtils.isNotEmpty(memberUserTagDOS)){
            List<Long> idList = memberUserTagDOS.stream().map(MemberUsertagDO::getId).collect(Collectors.toList());
            memberUsertagMapper.deleteBatchIds(idList);
        }
        createMemberUserTag(createReqVO,userId);
    }

    @Override
    public void updateUserTag(List<Long> memberUserTagRespVOS, Long userId) {
        if(CollectionUtils.isNotEmpty(memberUserTagRespVOS)){
            MemberUsertagExportReqVO exportReqVO = new MemberUsertagExportReqVO();
            exportReqVO.setUserId(userId);
            List<MemberUsertagDO> memberUserTagDOS = memberUsertagMapper.selectList(exportReqVO);
            if (CollectionUtils.isNotEmpty(memberUserTagDOS)){
                List<Long> idList = memberUserTagDOS.stream().map(MemberUsertagDO::getId).collect(Collectors.toList());
                memberUsertagMapper.deleteBatchIds(idList);
            }

            List<MemberUsertagCreateReqVO> createReqVO = new ArrayList<>();
            memberUserTagRespVOS.forEach(id -> {
                MemberUsertagCreateReqVO memberUsertagCreateReqVO = new MemberUsertagCreateReqVO();
                memberUsertagCreateReqVO.setUserId(userId);
                MemberLabelDO memberLabelDO = memberLabelMapper.selectById(id);
                memberUsertagCreateReqVO.setLabel(memberLabelDO.getLabel());
                memberUsertagCreateReqVO.setLabelId(id);
                createReqVO.add(memberUsertagCreateReqVO);
            });
            List<MemberUsertagDO> userTagDOS = MemberUsertagConvert.INSTANCE.convert(createReqVO);
            Collection<MemberUsertagDO> tagDOS = new ArrayList<>(userTagDOS);
            memberUsertagMapper.insertBatch(tagDOS);
        }
    }

    @Override
    public void updateMemberUserTagList(List<MemberUsertagRespVO> memberUserTagRespVOS,Long userId) {
        MemberUsertagExportReqVO exportReqVO = new MemberUsertagExportReqVO();
        exportReqVO.setUserId(userId);
        List<MemberUsertagDO> memberUserTagDOList = memberUsertagMapper.selectList(exportReqVO);
        if (CollectionUtils.isNotEmpty(memberUserTagDOList)){
            List<Long> idList = memberUserTagDOList.stream().map(MemberUsertagDO::getId).collect(Collectors.toList());
            memberUsertagMapper.deleteBatchIds(idList);
        }

        // 插入
        List<MemberUsertagDO> memberUserTagDOS = MemberUsertagConvert.INSTANCE.convertUserTagList(memberUserTagRespVOS);
        memberUserTagDOS.forEach(memberUsertagDO -> {
            memberUsertagDO.setUserId(userId);
        });
        Collection<MemberUsertagDO> userTagDOS = new ArrayList<>(memberUserTagDOS);
        memberUsertagMapper.insertBatch(userTagDOS);
    }


    @Override
    public void deleteMemberUsertag(Long id) {
        // 校验存在
        validateMemberUsertagExists(id);
        // 删除
        memberUsertagMapper.deleteById(id);
    }

    private void validateMemberUsertagExists(Long id) {
        if (memberUsertagMapper.selectById(id) == null) {
            throw exception(MEMBER_USERTAG_NOT_EXISTS);
        }
    }

    @Override
    public MemberUsertagDO getMemberUsertag(Long id) {
        return memberUsertagMapper.selectById(id);
    }

    @Override
    public List<MemberUsertagDO> getMemberUsertagList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberUsertagMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberUsertagDO> getMemberUsertagPage(MemberUsertagPageReqVO pageReqVO) {
        return memberUsertagMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberUsertagDO> getMemberUsertagList(MemberUsertagExportReqVO exportReqVO) {
        return memberUsertagMapper.selectList(exportReqVO);
    }

}
